/*
 * Copyright (c) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { SwitchStatement, rule, BreakStatement, is } from "qvog-dsl";

const SEVERITY = 2;
const DOC_PATH = 'docs/switch-break-check.md';
const DESCRIPTION = 'The default behavior in a `switch` statement is fall-through, which means that after a `case` is executed, if no `break` is encountered, the program will continue executing the next `case` (or `default`) and its corresponding code, even if the condition does not match. This is usually not the desired behavior.';

export default rule()
    .match(SwitchStatement)
    .when((node): boolean => {
        for (const clause of node.caseBlock.clauses) {
            if (!is(clause.statements[clause.statements.length - 1], BreakStatement)) {
                return true;
            }
        }
        return false;
    })
    .report({
        severity: SEVERITY,
        description: DESCRIPTION,
        docPath: DOC_PATH,
    });
